<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
                 type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=500931
-->
<window title="Mozilla Bug 500931"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=403005"
     target="_blank">Mozilla Bug 403005</a>
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=409298"
     target="_blank">Mozilla Bug 409298</a>
  </body>

  <!-- test code goes here -->
  <script type="application/javascript"><![CDATA[

    /** Test for Bug 403005 and 409298 **/

  SimpleTest.waitForExplicitFinish();

  function go()
  {
    ok(window instanceof Object, "window is instanceof Object");

    let wrap = $('ifr').contentWindow.wrappedJSObject;
    ok(!('obj' in XPCNativeWrapper(wrap)), "XPCNativeWrapper constructor works");
    let iwin = $('ifr').contentWindow;
    is(iwin.document.ELEMENT_NODE, 1, 'constants work through XrayWrapper');
    is(iwin.document.nodeName, "#document", 'attributes work through XrayWrappe

    location.foopy = 3;

    var xow_answer = [];
    for (let i in location)
        xow_answer.push(i);

    var xpcnw_answer = [];
    var xpcnw = new XPCNativeWrapper(location);
    xpcnw.barpy = 4;
    for (let i in xpcnw)
        xpcnw_answer.push(i);

    var expected = [
        "hash",
        "host",
        "hostname",
        "href",
        "pathname",
        "port",
        "protocol",
        "search",
        "reload",
        "replace",
        "assign",
        "foopy"
    ];

    var xpcnw_expected = expected.slice(0, expected.length - 1);
    xpcnw_expected.push("barpy");

    is(xow_answer.sort().toString(),
       expected.sort().toString(),
       'enumeration over XOWs walks the prototype chain');

    is(xpcnw_answer.sort().toString(),
       xpcnw_expected.sort().toString(),
       'enumeration over XPCNWs walks the prototype chain');

    var for_each_expected = [];
    for each (let i in new XPCNativeWrapper(location))
        for_each_expected.push(typeof i);

    var for_each_answer = [];
    for each (let i in Iterator(new XPCNativeWrapper(location)))
        for_each_answer.push(typeof i);

    is(for_each_answer.sort().toString(),
       for_each_expected.sort().toString(),
       'wrapper iterators are properly iterators');

    let sjow_answer = [];
    let (obj = { a: 3, next:1 }) {
        for (let i in wrap.to_iterate)
            sjow_answer.push(i);
        is(sjow_answer.sort().toString(), ['a', 'next'].sort().toString(),
           'enumeration over SJOWs walks the prototype chain');
    }

    sjow_answer = [];
    for (let i in wrap.location)
        sjow_answer.push(i);

    is(sjow_answer.sort().toString(),
       expected.sort().toString(),
       'enumeration over SJOWs walks the prototype chain and works over XOWs');

    for (let i in wrap.enumerate) {
        is(typeof i, "string", "enumeration on wrappers only returns strings");
    }

    isnot(wrap, new XPCNativeWrapper(iwin),
         'SJOWs equality hook returns false correctly against XPCNW');

    is(typeof(wrap.func), 'function',
       'SJOWs look like functions when they wrap functions');
    is(typeof(wrap.o), 'object',
       'SJOWs look like objects when they wrap objects');
    ok(wrap.o === wrap.o,
       "Identity holds when accessing the same object twice");

    var origProto = iwin.__proto__;
    try {
        iwin.__proto__ = iwin;
        ok(false, 'cyclic proto value allowed');
        iwin.__proto__ = origProto;
    } catch (e) {
        is(e.toString(),
           'TypeError: cyclic __proto__ value',
           'throw the right exception for a cyclic proto');
        is(iwin.__proto__, origProto, 'reset __proto__ after a cyclic proto');
    }

    try {
        is('PASS', iwin.eval("'PASS'"), 'iwin.eval throws an exception');
    } catch (e) {
        ok(false, 'iwin.eval does not throw an exception');
    }

    try {
        new XPCNativeWrapper("");
        ok(false, "How did we construct a wrapper around a primitive?");
    } catch (e) {
        ok(true, "Unable to new XPCNativeWrapper(primitive)");
    }

    try {
        is(XPCNativeWrapper(""), "", "XPCNativeWrapper as a function allows primitives");
    } catch (e) {
        ok(false, "Unable to wrap a primitive, even without 'new'");
    }

    // Some tests for SJOWs too.
    isnot(wrap.document.body != document.body, "body should not be the same");

    XPCNativeWrapper.prototype = {};
    let (w = new XPCNativeWrapper(iwin)) {
        ok(iwin.location, "able to set XPCNativeWrapper.prototype and do stuff with it");
    }

    is(new XPCNativeWrapper(iwin, Window).closed, false,
       "able to wrap a window in a window XPCNativeWrapper");
    try {
        new XPCNativeWrapper(document, Window);
        todo(false, "Able to wrap a document in a Window XPCNativeWrapper?")
    } catch (e) {
        ok(/ILLEGAL_VALUE/(e), "not able to wrap a document in a Window XPCNativeWrapper");
    }

    let (w = new XPCNativeWrapper($('ifr').contentWindow)) {
        w.foopybar = 5;
        ok(!("foopybar" in iwin), "XPCNativeWrappers allow expandos through");
        is(w.foopybar, 5, "can set expandos on XPCNativeWrappers, though");

        ok(delete w.foopybar, "deleting properties returns true correctly");
        ok(!("foopybar" in w), "Can delete properties from XPCNativeWrappers");

        is(w.window, iwin, "w.window exists and is the window");
        ok(delete w.iwin, "can delete builtin properties");
        is(w.window, iwin, "w.window is automatically recreated");

        iwin.foopy = 5;
        ok(delete w.foopy, "delete returns true");
        is(iwin.foopy, 5, "delete doesn't delete underlying properties");
        ok(delete iwin.foopy, "can delete window.foopy");
        ok(!("foopy" in iwin), "foopy is no longer in window");
    }

    try {
      is((function(x) { return x+1; }).apply(this, wrap.a), 2,
         "able to call apply with an XPCSafeJSObjectWrapped array");
    } catch (e) {
        ok(false,
           "Unable to call apply() with a XPCSafeJSObjectWrapped array");
    }

    try {
        iwin.__proto__ = null;
        is(iwin.__proto__, null,
           "allowed to update iwin.__proto__ to null");
    } catch (e) {
        ok(false, "some crazy exception was thrown");
    }

    try {
        new XPCSafeJSObjectWrapped(<x />).foo;
        ok(false, "Allowed to wrap E4X in SJOWs?");
    } catch (e) {
    }

    SimpleTest.finish();
  }
  ]]></script>
  <iframe type="content"
          src="http://mochi.test:8888/tests/js/xpconnect/tests/mochitest/file_wrappers-2.html"
          onload="go()"
          id="ifr">
  </iframe>
</window>
